package controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import org.springframework.beans.factory.annotation.Required;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.portlet.ModelAndView;
import org.springframework.web.portlet.bind.annotation.RenderMapping;

import service.MovieInfoService;
import domain.Movies;

@Controller
@RequestMapping("VIEW")
public class AddMovieController {
	
	@RenderMapping(params = "action=insertMovie")
	public ModelAndView handleRenderRequestInternal(RenderRequest request,
			RenderResponse response) throws Exception {
		
		Movies movie = new Movies();
		movie.setTitle(request.getParameter("movieTitle"));
		movie.setGenre(request.getParameter("movieGenre"));
		movie.setYear(request.getParameter("movieYear"));
		movie.setCountry(request.getParameter("movieCountry"));
		movie.setLanguages(request.getParameter("movieLanguages"));
		movie.setRating(request.getParameter("movieRating"));
		movie.setNumberofVotes(request.getParameter("movieVotes"));
		movie.setLinkToIMDB(request.getParameter("movieLink"));
		movieInfoService.addMovie(movie);
		
		List moviesList = movieInfoService.getMovies();
		Map<String, Object> model1 = new HashMap<String, Object>();
		model1.put("moviesList", moviesList);
		return new ModelAndView("view", "moviesList", moviesList);
	}
	
	private MovieInfoService movieInfoService;

	@Resource(name = "movieService")
	@Required
	public void setMovieService(MovieInfoService movieInfoService) {
		this.movieInfoService = movieInfoService;
	}
}
